package com.xgc.erp.info.supplier.ctrl;

import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.Record;
import com.xgc.common.base.BaseController;
import com.xgc.common.config.Constan;
import com.xgc.common.model.Supplier;
import com.xgc.common.routes.ControllerBind;
import com.xgc.common.vo.Feedback;
import com.xgc.erp.info.supplier.service.SupplierService;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;

@ControllerBind(path="/erp/info/supplier")
public class SupplierController extends BaseController {
    @Inject
    private SupplierService service;

    public void index() {
        render("index.html");
    }

    public void list(){
        Record record = new Record();
        renderJson(service.queryForList(getParaToInt("pageNumber",1), getParaToInt("pageSize",10),record,"order by create_time desc"));
    }

    public void add() {
        render("add.html");
    }

    public void save() {
        Supplier supplier=getBean(Supplier.class);
        Integer supplierId = service.getPrimaryKey(Constan.SEQ_SUPPLIER);
        supplier.setSupplierId(supplierId);
        supplier.setCreator(getVisitor().getCode());
        supplier.setCreateTime(new Date());
        boolean flag = service.isExit(service.getPK(),supplierId.toString());
        if(flag){
            setException("主键已存在");
        }else {
            flag = service.save(supplier);
            if(!flag) {
                setException("保存失败");
            }
        }
        render("add.html");
    }

    public void edit() throws UnsupportedEncodingException {
        Supplier supplier =(Supplier) service.findById(URLDecoder.decode(getPara("primaryKey"),"utf-8"));
        setAttr("supplier", supplier);
        render("edit.html");
    }

    public void update() {
        Supplier supplier=getBean(Supplier.class);
        supplier.setModifier(getVisitor().getCode());
        supplier.setUpdateTime(new Date());
        service.update(supplier);
        setAttr("supplier", supplier);
        render("edit.html");
    }

    public void delete() {
        service.deleteByIds(getIds());
        renderJson(Feedback.success());
    }

}
